(*


                         Sample dataComet-Secure AppleScript

_____________________________________________________________________________

capturecommand.script -- an example of dataComet session scripting

This script opens a dataComet session document, 
executes a command, copies and strips the output, logs off, 
and then closes the session window.

This script contains a number of routines you may find useful 
in other dataComet scripts, including:

   doconnect(sessionname, login, password)
   sendcommand(command)
   waitprompt(promptstring)
   waitmacro()
   logout()
   closewindow(sessionname)

This script works best when you use a dataComet session with the VT100
emulator set to 1 row X 1024 (or more) columns, allowing you to capture
command output without wrapping of the output and without extra lines in
the buffer.  You can also use the commands provided in more standard 24X80 
sessions when performing more conventional screen-scraping chores...

You may have to change the session's CR -> CR/LF mapping
in the "Emulator" dialog to get "sendcommand" to work 
correctly.

This script uses a sample Shell session.  You can use
the "doconnect" command to connect to a Telnet host.  
If you use "doconnect", you will need to SET the following 
variables before you can use the script to connect to a host:

   youruserid
   yourpassword
   yourhostprompt

*)

global theapp
-- useless since "tell application" won't accept it!  
-- Instead we must use "dataComet-Secure" as a literal

global foldername
global sessionname

global youruserid
global yourpassword
global yourhostprompt

on run
	set foldername to "" -- WAS ":Sessions:" set to document's home folder
	set sessionname to "ScriptExample.1X1024" -- document name
	
	set youruserid to "" -- SET to your user name
	set yourpassword to "" -- SET to your password
	set yourhostprompt to "$"
	-- SET to your host's prompt:  
	-- the complete string will work best
	
	tell application "dataComet-Secure"
		targetwindow "frontwindow"
		
		-- doconnect(foldername & sessionname, youruserid, yourpassword)
		--    use this command to connect to a host
		
		execute "!FX" & sessionname
		-- opens using dataComet macro as an alternative to using
		--    tell application "dataComet-Secure" to open file sessiondocument 
		--   which requires a complete pathname
		-- execute "!Fx" & sessionname
		--   use this command to open a session in your "Sessions" folder
		targetwindow sessionname
		
	end tell
	
	
	-- waitprompt(yourhostprompt)
	
	clearbuffer()
	sendcommand("ls -l | sort +0 -1 +3rn -4")
	waitprompt(yourhostprompt)
	
	set commandresult to copyresult(2, 2)
	-- 2, 2 strips final command line and CR
	
	logout()
	-- closewindow(sessionname)
	
	set junk to commandresult
	-- shows you the commandresult in the "result" window
	-- presumably at this point you want to paste 
	-- the result into another application
	
end run


-- open a host session using an existing dataComet document

on doconnect(sessiondocument, loginname, password)
	-- the login macro following will execute when we connect
	tell application "dataComet-Secure"
		targetwindow "frontwindow"
		-- resets target in case we're running the same script again
		-- and the previous target window closed and is now invalid
		
		execute "!Fx" & sessiondocument
		-- opens using dataComet macro as an alternative to using
		--    tell application "dataComet-Secure" to open file sessiondocument 
		-- which requires a complete pathname
		
		targetwindow
		-- target newly opened session so it's used 
		-- rather than the front window
		
		-- wait for login and password prompts and respond
		
		execute "!Z\\255:!D\\001"
		execute loginname & "
"
		execute "!Z\\255:!D\\001"
		execute password & "
"
	end tell
end doconnect


-- wait for a string to be sent by the host

on waitprompt(hostprompt)
	tell application "dataComet-Secure"
		execute "!Z\\255" & hostprompt & "!D\\001"
	end tell
	waitmacro()
end waitprompt


-- clear a dataComet session's scrollback buffer

on clearbuffer()
	tell application "dataComet-Secure"
		execute "!\\002\\006" -- clear buffer
	end tell
end clearbuffer


-- send a string to a dataComet window

on sendcommand(hostcommand)
	tell application "dataComet-Secure"
		-- send as plaintext to avoid ! and \ interpretation 
		-- by the macro interpreter
		send hostcommand & "
"
	end tell
end sendcommand


-- copy the scrollback buffer "paragraphs"; 
-- (start = 1 & endoffset = 0) == all

on copyresult(start, endoffset)
	tell application "dataComet-Secure"
		execute "!EA" -- select whole buffer
		copy
	end tell
	
	set thecopy to paragraphs start through 
		((count of paragraphs in result) - endoffset) of the result
	-- returns a list
	-- NOTE the characer at the end of "through" is the "Option-L" 
	-- Script Editor escape character so the CR formatting doesn't
	-- break the command!  (Just in case you're using non-Mac ASCII)
	
	-- convert the list back into text
	set editcopy to ""
	repeat with theline in thecopy
		set editcopy to editcopy & (theline as text) & "
" as text
	end repeat
	return (editcopy)
end copyresult


-- wait till dataComet is "Waiting" for input rather than 
-- "Executing" a macro

on waitmacro()
	set waiting to false
	repeat until waiting is true
		getstatus()
		if the result contains "Waiting" then
			set waiting to true
		end if
	end repeat
end waitmacro


-- get dataComet session status

on getstatus()
	tell application "dataComet-Secure"
		status
	end tell
	
	set status to the result
	return status
end getstatus


-- logout from a session

on logout()
	if getstatus() contains "Closed" then
		return
	end if
	
	if getstatus() contains "Connected" then
		sendcommand("logout")
	end if
	
	repeat while true
		if getstatus() contains "Closed" then
			return
		end if
	end repeat
end logout


-- close a dataComet document window by name

on closewindow(sessionname)
	tell application "dataComet-Secure"
		execute "!WS" & sessionname & "\\000"
		execute "!\\001\\003" -- Close
	end tell
end closewindow


-- bring a dataComet window to the front

on frontwindow(sessionname)
	tell application "dataComet-Secure"
		execute "!WS" & sessionname & "\\000"
	end tell
end frontwindow


(*
_____________________________________________________________________________
*)
